import { Instrusion } from "@/Battle/Intrusion.js";
import CombatEventBus from "@/Battle/CombatEventBus.js";
import { BattleEventType } from "@/Battle/Event/BattleEvent.js";

class _BattleInput extends Instrusion{
    constructor(){
        super();
        this.skipAuto=false;
    }

    onload(){
        CombatEventBus.on(
            BattleEventType.Action.Prepare.Request,
            this.processInput
        );
    }

    processInput=(event)=>{
        let action=event.payload.action;
        let subject=action.subject();
        
        if(subject.isActor()&&
        !this.skipAuto&&
        !subject.isAutoBattle()&&
        !subject.isConfused()){
            this.emit('action-prepare',action);
            event.interrupt();
        }
    }

    onunload(){
        BattleInput.onlyInput=null;
        CombatEventBus.off(
            BattleEventType.Action.Prepare.Request,
            this.processInput
        );
    }
}

export class BattleInput{
    /**
     * @type { _BattleInput }
     */
    static onlyInput=null;
    constructor(){
        if(!BattleInput.onlyInput){
            BattleInput.onlyInput=new _BattleInput();
        }
        return BattleInput.onlyInput;
    }
}